package com.egglazy.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egglazy.Entity.User;
import com.egglazy.Service.UserService;
import com.egglazy.Mapper.UserMapper;
import com.egglazy.Utils.Result;
import com.egglazy.Utils.ResultUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;

/**
* @author iron
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-05-22 14:02:08
*/
@Service
public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public User login(String username, String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getPassword, password);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Result<String> register(User user) {
        // 检查用户名是否已存在
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername()).eq(User::getStatus,1);
        if (baseMapper.selectCount(wrapper) > 0) {
            return ResultUtil.fail("用户名已存在");
        }
        // 保存用户
        if(baseMapper.insert(user) > 0){
            return ResultUtil.success("注册成功");
        }
        return ResultUtil.success("注册失败！");
    }

}




